package com.yb.web.controller.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yb.framework.core.domain.AjaxObject;
import com.yb.framework.core.domain.PageResult;
import com.yb.framework.core.utils.StringUtil;
import com.yb.framework.log.annotation.Log;
import com.yb.framework.log.enums.OperateTypeEnum;
import com.yb.module.db.domain.DbTable;
import com.yb.web.controller.base.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller 
@RequestMapping("/system/db/table")
@Api(tags = "数据表管理")
public class DbTabelController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(DbTabelController.class.getName());
	
	private static String DB_TABLE_ADD = "db/table/table_add";
	private static String DB_TABLE_EDIT = "db/table/table_edit";
	
	@GetMapping("/add_view")
	@PreAuthorize("hasAuthority('system:db:table:add')")
	public String addView() {
		return DB_TABLE_ADD;
	}
	
	@GetMapping("/edit_view")
	@PreAuthorize("hasAuthority('system:db:table:edit')")
	public String editView(DbTable table, ModelMap model) {
		model.put("table", tableService.getTableDetails(table));
		return DB_TABLE_EDIT;
	}
	
	/**
	 * 数据表列表
	 */
	@PostMapping("/list")
	@PreAuthorize("hasAuthority('system:db:table:list')")
	@ApiOperation(value="分页查询", notes="分页查询数据表信息")
	public @ResponseBody PageResult list(DbTable table) {
		startPage();
		return getPageInfo(tableService.getTableList(table));
	}
	
	/**
	 * 新增数据表
	 */
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('system:db:table:add')")
	@ApiOperation(value="新增数据表", notes="新增数据表信息")
	@Log(label = "新增数据表", operateType = OperateTypeEnum.ADD)
	public @ResponseBody AjaxObject add(DbTable table){
		tableService.createTable(table);
		return AjaxObject.newOk("新增数据表["+ table.getTableName() +"]，操作成功");
	}
	
	/**
	 * 编辑数据表
	 */
	@PostMapping("/edit")
	@PreAuthorize("hasAuthority('system:db:table:edit')")
	@ApiOperation(value="编辑数据表", notes="编辑数据表信息")
	@Log(label = "编辑数据表", operateType = OperateTypeEnum.EDIT)
	public @ResponseBody AjaxObject edit(DbTable table) {
		tableService.alterTable(table);
		return AjaxObject.newOk("编辑数据表["+ table.getTableNameNew() +"]，操作成功");
	}
	
	/**
	 * 删除数据表
	 */
	@PostMapping("/delete")
	@PreAuthorize("hasAuthority('system:db:table:delete')")
	@ApiOperation(value="删除数据表", notes="批量删除数据表信息")
	@Log(label = "删除数据表", operateType = OperateTypeEnum.DELETE)
	public @ResponseBody AjaxObject delete(@RequestParam("dbNames") String dbNames, 
			@RequestParam("tableNames") String tableNames) {
		tableService.dropTables(StringUtil.str2StringArray(dbNames)[0], 
				StringUtil.str2StringArray(tableNames));
		return AjaxObject.newOk("删除数据表["+ tableNames +"]，操作成功");
	}
}
